package com.pengliu.common.mybatis.page;

import java.io.Serializable;
import java.util.List;

/**
 * user: pengliu
 * date: 2017年10月17日
 * desc:
 * @author pengliu
 */
public final class Page<M> implements Serializable {

    /**
     * 默认的序列化版本 id.
     */
    private static final long serialVersionUID = 1L;
    /**
     * 分页查询开始记录位置.
     */
    private int begin;
    /**
     * 分页查看下结束位置.
     */
    private int end;
    /**
     * 每页显示记录数.
     */
    private int pageSize = 5;
    /**
     * 查询结果总记录数.
     */
    private int total;
    /**
     * 当前页码.
     */
    private int pageNo=1;
    /**
     * 总共页数.
     */
    private int pages;

    private List<M> result;

    public Page() {
    }

    /**
     * 构造函数.
     *
     * @param begin
     * @param pageSize
     */
    public Page(int begin, int pageSize) {
        this.begin = begin;
        this.pageSize = pageSize;
        this.end = this.begin + this.pageSize;
        this.pageNo = (this.begin / this.pageSize) + 1;
    }

    /**
     * @param begin
     * @param pageSize
     * @param total
     */
    public Page(int begin, int pageSize, int total) {
        this(begin, pageSize);
        this.total = total;
    }

    /**
     * 设置页数，自动计算数据范围.
     *
     * @param pageNo
     */
    public Page(int pageNo) {
        this.pageNo = pageNo;
        pageNo = pageNo > 0 ? pageNo : 1;
        this.begin = this.pageSize * (pageNo - 1);
        this.end = this.pageSize * pageNo;
    }

    public int getOffset() {
        return (pageNo-1)* pageSize;
    }

    /**
     * @return the begin
     */
    public int getBegin() {
        return begin;
    }

    /**
     * @return the end
     */
    public int getEnd() {
        return end;
    }

    /**
     * @param end the end to set
     */
    public void setEnd(int end) {
        this.end = end;
    }

    /**
     * @param begin the begin to set
     */
    public void setBegin(int begin) {
        this.begin = begin;
        if (this.pageSize != 0) {
            this.pageNo = (this.begin / this.pageSize) + 1;
        }
    }

    /**
     * @return the pageSize
     */
    public int getPageSize() {
        return pageSize;
    }

    /**
     * @param pageSize the pageSize to set
     */
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    	if (this.begin != 0) {
    		this.pageNo = ( this.begin / this.pageSize) + 1;
    	}
        
    }

    /**
     * @return the totalRecords
     */
    public int getTotal() {
        return total;
    }

    /**
     * @param total the total to set
     */
    public void setTotal(int total) {
        this.total = total;
        if(0 ==  pageSize) {
        	//如果输入每页显示条数为0，那么只有一页等于总条数
        	this.pageSize = this.total;
        }
        this.pages = total / this.pageSize;
        if (this.total % this.pageSize != 0) {
            this.pages =this.pages+1;
        }
    }

    /**
     * @return the pageNo
     */
    public int getPageNo() {
        return pageNo;
    }

    /**
     * @param pageNo the pageNo to set
     */
    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
        pageNo = pageNo > 0 ? pageNo : 1;
        this.begin = this.pageSize * (pageNo - 1);
        this.end = this.pageSize * pageNo;
    }

    /**
     * @return the pages
     */
    public int getPages() {
        if (pages == 0) {
            return 1;
        }
        return pages;
    }

    /**
     * @param pages the pages to set
     */
    public void setPages(int pages) {
        this.pages = pages;
    }

    public List<M> getResult() {
        return result;
    }

    public void setResult(List<M> result) {
        this.result = result;
    }

    @Override
    public String toString() {
        final StringBuilder builder = new StringBuilder("begin=").append(begin).append(", end=")
                .append(end).append(", pageSize=").append(pageSize).append(", total=").append(
                        total).append(", pageNo=").append(pageNo).append(", pages=")
                .append(pages);

        return builder.toString();
    }
}